package com.herui.iot.ui.view;

import android.graphics.Color;
import android.graphics.Typeface;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.herui.sport_care.config.ChartConfig;
import com.herui.sport_care.utils.DateUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by amanda.shan on 2022/12/14.
 */
public class ReportLineChartView {

    private LineChart mDoubleLineChar;

    public ReportLineChartView(LineChart mDoubleLineChar) {
        this.mDoubleLineChar = mDoubleLineChar;
    }

    public void refresh(){
        mDoubleLineChar.invalidate();
    }

    public void init() {

        initStyle();

        //设置数值选择监听
//        mDoubleLineChar.setOnChartValueSelectedListener(this);
        // 没有描述的文本
        mDoubleLineChar.getDescription().setEnabled(false);
        // 支持触控手势
        mDoubleLineChar.setTouchEnabled(true);
        mDoubleLineChar.setDragDecelerationFrictionCoef(0.9f);
        // 支持缩放和拖动
        mDoubleLineChar.setDragEnabled(false);
        mDoubleLineChar.setScaleEnabled(false);
        mDoubleLineChar.setDrawGridBackground(false);
        mDoubleLineChar.setHighlightPerDragEnabled(true);
        // 如果禁用,扩展可以在x轴和y轴分别完成
        mDoubleLineChar.setPinchZoom(true);
        // 设置背景颜色(灰色)
        mDoubleLineChar.setBackgroundColor(Color.WHITE);
        //默认x动画
        mDoubleLineChar.animateX(0);
    }

    private void initStyle() {
        //x轴
        XAxis xAxis = mDoubleLineChar.getXAxis();
        xAxis.setTextSize(11f);
        xAxis.setTextColor(Color.BLACK);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawLabels(true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setEnabled(true);
        //自定义样式
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
                return DateUtils.sToMMss((int) value);
            }
        });
//        xAxis.setAxisMaximum(20);

        //左边y轴
        YAxis leftAxis = mDoubleLineChar.getAxisLeft();
        leftAxis.setTextColor(0xffF1917D);
        leftAxis.setTextSize(ChartConfig.yLableSize);
        leftAxis.setAxisMaximum(20f);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setDrawGridLines(true);
        leftAxis.setGranularityEnabled(true);
        leftAxis.setDrawLabels(true);

        //右边
        YAxis rightAxis = mDoubleLineChar.getAxisRight();
        rightAxis.setTextColor(0xff77DABD);
        rightAxis.setTextSize(ChartConfig.yLableSize);
        rightAxis.setAxisMaximum(20f);
        rightAxis.setAxisMinimum(0f);
        rightAxis.setDrawGridLines(false);
        rightAxis.setDrawZeroLine(false);
        rightAxis.setGranularityEnabled(false);

        setLegend();
    }

    public void setLegend(){
        //为了让大家更直观的看到效果，我这里设置了整个图形距离上下左右四个方向的边距
        //在后面会讲解
//        lc.setExtraOffsets(30,60,30,30);

        //得到Legend对象
        Legend legend = mDoubleLineChar.getLegend();
        legend.setDrawInside(false);//不绘制在图形内部
        legend.setTextSize(ChartConfig.legendLableSize);//设置文字大小
        legend.setTypeface(Typeface.DEFAULT_BOLD);//文字加粗
//        legend.setFormSize(50f);//设置图例的大小
//        legend.setTextColor(Color.BLUE);//设置文字颜色
//        legend.setFormToTextSpace(20f);//设置图例距离文字的距离
        legend.setForm(Legend.LegendForm.CIRCLE);//设置图例类型为线条
        legend.setYOffset(20f);//距离底部20dp
        //设置图例下、中对齐
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
    }

    private void initData(List<Entry> leftEntries, List<Entry> rightEntries, String leftLable,
                          String rightLable) {
        LineDataSet set1, set2;
        // 创建一个数据集,并给它一个类型
        set1 = new LineDataSet(leftEntries, leftLable);

        set1.setAxisDependency(YAxis.AxisDependency.LEFT);
        set1.isDrawValuesEnabled();
        set1.setColor(0xffF1917D);
        set1.setCircleColor(0xffF1917D);
        set1.setLineWidth(2f);
        set1.setCircleRadius(3f);
        set1.setFillAlpha(65);
        set1.setFillColor(0xffF1917D);
        set1.setDrawFilled(true);
        set1.setDrawCircleHole(false);

        //创建一个数据集,并给它一个类型
        set2 = new LineDataSet(rightEntries, rightLable);
        set1.isDrawValuesEnabled();
        set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
        set2.setColor(0xff77DABD);
        set2.setCircleColor(0xff77DABD);
        set2.setLineWidth(2f);
        set2.setCircleRadius(3f);
        set2.setFillAlpha(65);
        set2.setFillColor(0xff77DABD);
        set2.setDrawCircleHole(false);
        set2.setDrawFilled(true);

        // 创建一个数据集的数据对象
        LineData data = new LineData(set1, set2);
        data.setValueTextColor(Color.WHITE);
        data.setValueTextSize(9f);

        //设置数据
        mDoubleLineChar.setData(data);
    }


    public void setData(List<Entry> leftEntries, List<Entry> rightEntries, String leftLable,
                        String rightLable) {

        initData(leftEntries, rightEntries, leftLable, rightLable);

        LineDataSet lineDataSet = (LineDataSet) mDoubleLineChar.getData().getDataSetByIndex(0);
        lineDataSet.setValues(leftEntries);
        lineDataSet = (LineDataSet) mDoubleLineChar.getData().getDataSetByIndex(1);
        lineDataSet.setValues(rightEntries);

//        int size = leftEntries.size();
//        if (rightEntries.size() > size){
//            size = rightEntries.size();
//        }
//        if (size > 20) {
//            XAxis xAxis = mDoubleLineChar.getXAxis();
//            xAxis.setAxisMaximum(size);
//        }

        float leftMax = 0, rightMax = 0;
        for (int i = 0; i < leftEntries.size(); i++) {
            if (leftMax < leftEntries.get(i).getY()){
                leftMax = leftEntries.get(i).getY();
            }
        }
        for (int i = 0; i < rightEntries.size(); i++) {
            if (rightMax < rightEntries.get(i).getY()){
                rightMax = rightEntries.get(i).getY();
            }
        }

        YAxis yAxis = mDoubleLineChar.getAxisLeft();
        yAxis.setAxisMaximum(leftMax+5);

        yAxis = mDoubleLineChar.getAxisRight();
        yAxis.setAxisMaximum(rightMax+5);

        mDoubleLineChar.getData().notifyDataChanged();
        mDoubleLineChar.notifyDataSetChanged();

        refresh();
    }

}
